package net.lcheng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.lcheng.commons.utils.PageList;
import net.lcheng.dao.PCategoryAttributeMapper;
import net.lcheng.model.PCategoryAttribute;
import net.lcheng.model.PCategoryAttributeExample;
import net.lcheng.service.CategoryAttributeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by LV on 2016/6/27.
 */
@Service("categoryAttributeService")
public class CategoryAttributeServiceImpl implements CategoryAttributeService {

    @Resource
    private PCategoryAttributeMapper PCategoryAttributeMapper;

    /**
     * 添加属性
     * @param PCategoryAttribute category attribute entity
     * @return int
     */
    @Override
    public int addAttribute(PCategoryAttribute PCategoryAttribute) {

        return PCategoryAttributeMapper.insertSelective(PCategoryAttribute);
    }

    /**
     *查询当前分类的列表
     * @param categoryId category's id
     * @param pageIndex current page index
     * @param pageSize page's size
     * @return pageList
     */
    @Override
    public PageList<PCategoryAttribute> findAllCategoryAttributeByCategoryId(String categoryId, int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex,pageSize);

        //select all attribute list by categoryId
        PCategoryAttributeExample example=new PCategoryAttributeExample();
        example.createCriteria().andPCategoryCategoryIdEqualTo(categoryId);
        List<PCategoryAttribute> list =PCategoryAttributeMapper.selectByExample(example);

        PageInfo<PCategoryAttribute> pageInfo=new PageInfo<>(list);
        PageList<PCategoryAttribute> pageList=new PageList<>();
        pageList.list=pageInfo.getList();
        pageList.pageIndex=pageInfo.getPageNum();
        pageList.pageSize=pageInfo.getPageSize();
        pageList.totalPage=pageInfo.getPages();
        pageList.totalCount=pageInfo.getTotal();

        return pageList;
    }

    /**
     * 由主键查询分类属性
     * @param attributeId
     * @return
     */
    @Override
    public PCategoryAttribute findCategoryAttributeByPrimaryId(String attributeId) {
        return PCategoryAttributeMapper.selectByPrimaryKey(attributeId);
    }

    /**
     * 修改分类属性
     * @param attribute
     * @return
     */
    @Override
    public int updateCategoryAttributeByPrimaryId(PCategoryAttribute attribute) {
        return PCategoryAttributeMapper.updateByPrimaryKeySelective(attribute);
    }

    /**
     * 删除分类属性
     * @param attributeId
     * @return
     */
    @Override
    public int deleteCategoryAttributeByPrimaryId(String attributeId) {
        return PCategoryAttributeMapper.deleteByPrimaryKey(attributeId);
    }


}
